/**
 * Created by 15c on 2017/11/29.
 */
//获取页面跳转传过来的参数值
function request(params) {

    var url = location.href;

    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");

    var paraObj = {};

    for (i = 0; j = paraString[i]; i++) {

        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);

    }

    var returnValue = paraObj[params.toLowerCase()];

    if (typeof (returnValue) == "undefined") {

        return "";

    } else {

        return returnValue;

    }

}
//获取参数的函数调用模式；
//var name = decodeURI(request("travleId"));
//服务器地址
var url = 'http://www.xiaowenxiong.cn/test';
//服务器图片路径
var imgUrl = url + "/resources/img/";
//travelId为传过来的参数,当前游记id
var travelId = decodeURI(request("travleId"));



//用于登录账户发表评论
$("#commentSubmit").click(function () {
    //获得评价文本内容
    var txt = $("#commentTxt").val();
    console.log(txt);
    if (txt.length == 0) {
        alert("ok")
    } else {
        alert(txt)
    }
});
var userId;
//页面加载完成就显示
$(document).ready(function () {
    //userId为当前游记创建者id;
     userId =window.sessionStorage.getItem(loginUserId);
    getTravelInfo(travelId);
    getComments(travelId);
    if (userId) {
        getUserAvt(userId);

    } else {
        $("#view_avt").attr('src', '../../resources/travel-note/img/avt.jpg');

    }
    //点赞
    $("#like_view").click(function () {
        if ($("#like_view").attr("val") == 1) {
            addLike();
        } else {
            removeLike();
        }

    });
});
//点赞
function addLike() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/expressingLove',
        data: {
            travelId: travelId
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {

            if (result.error == 0) {
                var data = result.data;
                console.log(data);
                var i = $("#like_view").text();

                $("#like_view").text(parseInt(i, 10) + 1);
                $("#like_view").css('color', 'red');
                $("#like_view").attr("val", 2);
            } else {
                alert("点赞失败")
            }
        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });
}
//取消点赞
function removeLike() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/removingLove',
        data: {
            travelId: travelId
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {

            if (result.error == 0) {
                var data = result.data;
                console.log(data);
                var i = $("#like_view").text();
                $("#like_view").text(parseInt(i, 10) - 1);
                $("#like_view").css('color', '#494B49');
                $("#like_view").attr("val", 1);
            } else {
                alert("点赞失败")
            }
        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });
}


//获得登录成功用户信息，此处重点需要其头像
function getUserAvt(userId) {
    $.ajax({
        type: 'GET',
        url: url + '/users/details/show',
        data: {
            userId: userId
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {

            if (result.error == 0) {
                var data = result.data;
                console.log(data);
                if (data.hasOwnProperty("userAvatarAddress") && data.userAvatarAddress.length != 0) {
                    $("#view_avt").attr('src', 'http://www.xiaowenxiong.cn/test/resources/img/' + data.userAvatarAddress);
                    $("#view_avt_a").attr('href','../personCenter/person-view.html?userId='+userId);

                }
            } else {
                $("#view_avt").attr('src', '../../resources/travel-note/img/avt.jpg');
            }
        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });
}

//获取游记详情
function getTravelInfo(id) {
    $.ajax({
        type: 'GET',
        url: url + '/travels/travelInfo',
        data: {
            travelId: id
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {

            if (result.error == 0) {
                var data = result.data;
                var creator = data.travelCreatorUser;
                console.log(data);
                if (creator.hasOwnProperty("userAvatarAddress") && creator.userAvatarAddress.length != 0) {
                    $("#user_avt").attr("src", imgUrl + creator.userAvatarAddress);
                }
                $("#user_page_view").attr('href', '../personCenter/other-view.html?userId=' + data.travelCreatorUser.userId);
                $("#user_email").text(data.travelCreatorUser.userEmail);
                $("#travel_title").text(data.travelTitle);
                $("#travel_type").text(data.travelType.travelTypeContent);
                $("#issue_time").text(datetimeFormat_2(data.travelIssueTime));
                $("#like_view").text(data.travelLikeNum);
                $("#page_view").text(data.travelPageView);
                $("#travel_content").val(data.trsvelContent);
                if (data.hasOwnProperty("travelImageAddress")) {
                    var travelImageArray = new Array();
                    travelImageArray = (data.travelImageAddress).split(",");
                    for (var i = 0; i < travelImageArray.length; i++) {
                        $("#imgDiv").append(
                            "<img class='col-md-6 col-xs-6' src='" + imgUrl + travelImageArray[i] + "'>"
                        )
                    }
                    $('.default').remove();

                }
            } else {
                alert('请求失败，刷新试试');
            }
        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });
}
//获取该游记所有评论以及评论者信息
function getComments(id) {
    $.ajax({
        type: 'GET',
        url: url + '/comments/listComments',
        data: {
            travelId: id
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {

            if (result.error == 0) {
                var data = result.data;
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    var commentor = data[i].commentUserId;
                    var commentor_avt;
                    if (commentor.hasOwnProperty("userAvatarAddress") && commentor.userAvatarAddress.length != 0) {
                        commentor_avt = imgUrl + commentor.userAvatarAddress;
                    } else {
                        commentor_avt = '../../resources/travel-note/img/avt.jpg';
                    }
                        $("#commentContent").append(
                            //../../resources/travel-note/img/avt.jpg
                            "<ul  class='comment_ul review-con col-xs-12'><li><div class='media col-md-12 col-xs-12'><div class='col-md-2 col-xs-4'><img class='img-circle col-md-12 col-xs-12' style='height: 60px;width: 90px' src='" + commentor_avt + "'/></div> <div class='con-r'> <span class='col-md-9 col-xs-8'>" + data[i].commentContent + "</span> <div class='col-md-8 col-md-offset-2 col-xs-12'><a href='../personCenter/other-view.html?userId=" + data[i].commentUserId.userId + "'><span class='glyphicon glyphicon-user col-md-6 col-xs-6' style='color: rgb(73, 75, 73); font-size: 15px;'>" + data[i].commentUserId.userEmail + "</span></a><span class='glyphicon glyphicon-time col-md-6 col-xs-6' style='color: rgb(73, 75, 73); font-size: 15px;'> " + datetimeFormat_2(data[i].commentTime) + "</span></div></div></div></li> </ul>"
                        )

                }

            } else if (result.error == 113) {
                $("#commentContent").append(
                    "<ul  class='default_ul review-con col-xs-12'><li><div class='media col-md-12 col-xs-12'><div class='col-md-2 col-xs-4'></div> <div class='con-r'> <span class='col-md-9 col-xs-8'>暂无评论</span> <div></div></div></div></li> </ul>"
                )
            } else {
                alert('请求失败，刷新试试');
            }
        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });
}

//返回yyyy-MM-dd 00:00:00格式日期
function datetimeFormat_2(longTypeDate) {
    var datetimeType = "";
    var date = new Date();
    date.setTime(longTypeDate);
    datetimeType = date.getFullYear() + "-" + getMonth(date) + "-" + getDay(date) + " " + getHours(date) + ":" + getMinutes(date) + ":" + getSeconds(date);//yyyy-MM-dd 00:00:00格式日期
    return datetimeType;
}
//返回 01-12 的月份值
function getMonth(date) {
    var month = "";
    month = date.getMonth() + 1; //getMonth()得到的月份是0-11
    if (month < 10) {
        month = "0" + month;
    }
    return month;
}
//返回01-30的日期
function getDay(date) {
    var day = "";
    day = date.getDate();
    if (day < 10) {
        day = "0" + day;
    }
    return day;
}
//返回小时
function getHours(date) {
    var hours = "";
    hours = date.getHours();
    if (hours < 10) {
        hours = "0" + hours;
    }
    return hours;
}
//返回分
function getMinutes(date) {
    var minute = "";
    minute = date.getMinutes();
    if (minute < 10) {
        minute = "0" + minute;
    }
    return minute;
}
//返回秒
function getSeconds(date) {
    var second = "";
    second = date.getSeconds();
    if (second < 10) {
        second = "0" + second;
    }
    return second;
}